<?php
class GROUPS{
    //general variables
	var $dbErrorMsg = "Failed set/get data from database (Class GROUPS): ";

    /** Function for getting the group
     * @param  $groupId
     * @return void
     */
    function getGroup($groupId=0){    	
        if($groupId == 0){
        $sql="describe `".tblPrefix."groups`";
    		$tmpres=MAIN::returnArray($sql);
    		$res=array();
    		foreach($tmpres as $r){
    			if($r['Field']=='rights'){
    				$res[0][$r['Field']]=100;
    			}elseif($r['Field']=='id'){
    				$res[0][$r['Field']]=0;
    			}else{
    				$res[0][$r['Field']]='';
    			}    			
    		}
        }else{	    	
			$sql="select `".tblPrefix."groups`.*				
				from `".tblPrefix."groups`				 			 
				 where `".tblPrefix."groups`.`id`=".$groupId;
			$res=MAIN::returnArray($sql);
        }
        return $res[0];
    }
	/** Function for getting the group
     * @param  $groupId
     * @return void
     */
    function getGroupByRights($rights){    	
            	
		$sql="select `".tblPrefix."groups`.*				
				from `".tblPrefix."groups`				 			 
				 where `".tblPrefix."groups`.`rights`=".$rights;
		$res=MAIN::returnArray($sql);
        
        return @$res[0];
    }
	function getGroupName($rights){
		$rights=mysql_real_escape_string($rights);
		$sql="select * from `".tblPrefix."groups` WHERE `".tblPrefix."groups`.`rights`=".$rights;
		$res = MAIN::returnArray($sql);
		return @$res[0]['display_name'];
	}
	/** Function add/update Group
	 * @param : groupId, groupName
	 * @return: groupId 
	 */
    function setGroup($id,$DATA){
    	$name=mysql_real_escape_string($DATA['txt_name']);
    	$displayName=mysql_real_escape_string($DATA['txt_display_name']);
		$desc=mysql_real_escape_string($DATA['txt_desc']);
    	$rights=mysql_real_escape_string($DATA['ddl_rights']);
    	$sql="select `".tblPrefix."groups`.`id` from `".tblPrefix."groups` where `id`=".$id;
		$res=MAIN::returnArray($sql);    		
        if(count($res)>0){
        	//edit
        	$sql = "update `".tblPrefix."groups` set 
				`name`='".$name."',`display_name`='".$displayName."',`desc`='".$desc."',`rights`=".$rights.",
				`modif`=NOW() where `id`=".$id;
        }else{
        	//insert
        	$sql = "insert into `".tblPrefix."groups` (`name`,`display_name`,`desc`,`rights`,
										`modif`)
								 values ('".$name."','".$displayName."','".$desc."','".$rights."',
								 		NOW())";
        }
		MAIN::executeCommand($sql);
		if($id==0){
			$groupId=mysql_insert_id();
			$desc="NEW GROUP";
		}else{
			$groupId = $id;
			$desc="GROUP UPDATE";
		}
		LOGS::setLog('GROUPS', $desc, $groupId, MAIN::getIP());    	
		return $groupId;
    }	
	/** Function returning all groups in database table groups	 
	 * return: array (Groups)
	 */
    function getGroups(){
		$sql = "select * from `".tblPrefix."groups` order by `rights`";    	
		return MAIN::returnArray($sql);
	}	
	/**
	 * Function for removing group from database
	 * params : groupId
	 * return: void
	 */
    function delGroup($groupId){
    	//OBS!!! : can compromise the system ToDo : give specific rights for this fn
    	$sql="delete from `".tblPrefix."groups` where `id`=".$groupId;
		return MAIN::executeCommand($sql);
    }
}
?>